package com.kk.xhr.admin.biz.impl;

import com.kk.xhr.admin.biz.ITrainBizService;
import com.kk.xhr.admin.controller.request.TrainAddRequest;
import com.kk.xhr.admin.controller.request.TrainModifyRequest;
import com.kk.xhr.admin.controller.request.TrainQueryRequest;
import com.kk.xhr.admin.controller.vo.TrainVO;
import com.kk.xhr.common.pojo.TrainStatusEnum;
import com.kk.xhr.model.dto.PageDto;
import com.kk.xhr.model.dto.TrainDTO;
import com.kk.xhr.model.dto.TrainQueryRequestDTO;
import com.kk.xhr.model.service.INotificationService;
import com.kk.xhr.model.service.ITrainService;
import com.kk.xhr.model.service.IUserService;
import com.kk.xhr.util.DateUtil;
import com.kk.xhr.web.vo.PageVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 培训biz服务接口实现
 *
 * @author kkmystery
 * @version 1.0 2021/3/31
 * @since 1.0.0
 */
@Service
@RequiredArgsConstructor
public class TrainBizServiceImpl implements ITrainBizService {
    private final ITrainService trainService;
    private final IUserService userService;
    private final INotificationService notificationService;

    /**
     * {@inheritDoc}
     */
    @Override
    public PageVO<TrainVO> queryPage(TrainQueryRequest request) {
        PageDto<TrainDTO> dtos = trainService.queryPage(TrainQueryRequestDTO.builder()
            .userId(request.getUserId())
            .status(request.getStatus())
            .limit(request.getLimit())
            .offset(request.getOffset())
            .build());
        if (0 == dtos.getTotal()) {
            return PageVO.empty();
        }
        return PageVO.fromDTO(dtos, dto -> TrainVO.builder()
            .id(dto.getId())
            .name(userService.queryName(dto.getUserId()))
            .content(dto.getContent())
            .remark(dto.getRemark())
            .status(dto.getStatus())
            .trainDate(dto.getTrainDate())
            .build());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<TrainVO> queryMine(Long userId) {
        String name = userService.queryName(userId);
        return trainService.query(userId)
            .stream()
            .map(dto -> TrainVO.builder()
                .id(dto.getId())
                .name(name)
                .content(dto.getContent())
                .remark(dto.getRemark())
                .status(dto.getStatus())
                .trainDate(dto.getTrainDate())
                .build())
            .collect(Collectors.toList());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(TrainAddRequest request) {
        if (request.getUserIds().isEmpty()) {
            return;
        }
        List<TrainDTO> list = request.getUserIds().stream().map(userId -> TrainDTO.builder()
            .userId(userId)
            .content(request.getContent())
            .remark(request.getRemark())
            .trainDate(DateUtil.validTime(request.getTrainDate()))
            .status(TrainStatusEnum.NOT_BEGIN)
            .build())
            .collect(Collectors.toList());
        trainService.batchAdd(list);
        String content = "您有新的培训, 请前往我的培训查看";
        notificationService.batchAdd(request.getUserIds(), content);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void modify(Long id, TrainModifyRequest request) {
        trainService.modify(id, TrainDTO.builder()
            .content(request.getContent())
            .remark(request.getRemark())
            .trainDate(DateUtil.validTime(request.getTrainDate()))
            .build());
    }

    @Override
    public void modifyStatus(Long id, TrainStatusEnum status) {
        trainService.modifyStatus(id, status);
    }
}
